<!--
* 修改页面
*-->
<html>
<head>
<script type="text/javascript" src="../../libs/jquery.min.js"></script>
<script type="text/javascript" src="../../libs/path.js"></script>
<meta charset="utf-8">
<title>修改</title>
<link rel="stylesheet" href="../../libs/layui/css/layui.css" media="all">
<link rel="stylesheet" href="../../libs/ztree/css/metroStyle/metroStyle.css">
</head>
<body >
<div class="layui-field-box">
    <form class="layui-form" action="">
        <input type="hidden" id="roleId" name="roleId" value="">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称<span class="span_must">*</span></label>
            <div class="layui-input-normal">
                <input type="text" id="name"  name="name" value=""  maxlength="10"  lay-verify="required" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色编码<span class="span_must">*</span></label>
            <div class="layui-input-normal">
                <input type="text" id="code"  name="code" value=""   maxlength="10" lay-verify="required" placeholder="请输入备注" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" >
            <label class="layui-form-label">授权<span class="label_span">:</span></label>
            <div class="layui-input-inline" >
                    <ul id="menuTree" class="ztree" ></ul>
            </div>
        </div>
        <div class="page-footer">
            <div class="btn-list">
                <div class="btnlist">
                    <button class="layui-btn" lay-submit="" lay-filter="roleSubmit" ><i class="fa fa-floppy-o">&nbsp;</i>保存</button>
                </div>
            </div>
        </div>
    </form>
</div>
<script src="../../libs/jquery.min.js"></script>
<script src="../../libs/ztree/jquery.ztree.all.min.js"></script>
<script src="../../libs/layui/layui.js"></script>
<script>
    var roleId = getParam("id");
    $("#roleId").val(roleId);
    $("#name").val(decodeURI(getParam("name")));
    $("#code").val(decodeURI(getParam("code")));

    layui.use(['form'], function () {
        var form = layui.form();

        //监听提交
        form.on('submit(roleSubmit)', function (data) {
            //获取选择的菜单
            var nodes = ztree.getCheckedNodes(true);
            var cdData = "";
            for(var i=0; i<nodes.length; i++) {
                cdData = cdData + nodes[i].id +","
            }
            var url ="../../updateRole";
            $.ajax({
                type: "post",
                url: url,
                data: {"name":data.field.name,"roleId":data.field.roleId,"cdData":cdData,"code":data.field.code},
                async: false,
                success: function (R) {
                    if (R === 0) {
                        layer.msg('操作成功！');
                    } else {
                        layer.msg('修改异常！');
                    }
                },
                error: function () {
                    alert("系统繁忙");
                }
            });
            return false;
        });


    });

    /*下拉树*/
    var setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: -1
            },
            key: {
                url:"nourl"
            }
        },
        check:{
            enable:true,
            nocheckInherit:true
        }
    };
    var ztree;
    //加载菜单树
    $.ajax({
        //url: "/sys/menu/list",
        type: "post",
        url:'../../getTreeList',
        contentType: "application/json",
        async: false,
        dataType: "json",
        success: function (R) {
                ztree = $.fn.zTree.init($("#menuTree"), setting, R.data);
                //展开所有节点
                ztree.expandAll(true);
        },
        error: function () {
            alert("系统错误");
        }
    });
	//勾选已选中的菜单
    $.ajax({
        type: "post",
        url:'../../getUserRoleList?id='+roleId,
        contentType: "application/json",
        async: false,
        dataType: "json",
        success: function (R) {
        	if(R.code === 200){
        		//勾选角色所拥有的菜单
                var menuIds = R.data;
                for(var i=0; i<menuIds.length; i++) {
                    var node = ztree.getNodeByParam("id", menuIds[i].id);
                    if(node!=null){
                        ztree.checkNode(node, true, false);
                    }
                }
        	}else{
        		alert(R.msg);
        	}
        },
        error: function () {
            alert("系统错误");
        }
    });
    
    function getParam(paramName) { 
        paramValue = "", isFound = !1; 
        if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) { 
            arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0; 
            while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++ 
        } 
        return paramValue == "" && (paramValue = null), paramValue 
    }

</script>
</body>
</html>
